﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using UtilZ.Dotnet.Ex.Base;

namespace WpfApp.Test
{
    /// <summary>
    /// TestThreadEx.xaml 的交互逻辑
    /// </summary>
    public partial class TestThreadEx : Window
    {
        public TestThreadEx()
        {
            InitializeComponent();
        }

        private ThreadEx _thread = null;
        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            if (this._thread == null)
            {
                this._thread = new ThreadEx(this.ThreadMethod, "xxx", true);
            }

            if (this._thread.IsRuning)
            {
                bool ret = this._thread.SyncStop(5000);
                MessageBox.Show(ret.ToString());
                btnStart.Content = "启动";
            }
            else
            {
                this._thread.Start();
                btnStart.Content = "停止";
            }
        }

        private void ThreadMethod(ThreadPara threadPara)
        {
            while (!threadPara.Token.IsCancellationRequested)
            {
                threadPara.WaitOne(200);
            }

            Thread.Sleep(3000);
        }
    }
}
